package dianmobile.eistarandroid.network.request;

import android.content.Context;

import com.android.volley.Response;

import org.xmlpull.v1.XmlPullParser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import dianmobile.eistarandroid.beans.MailEntity;
import dianmobile.eistarandroid.utils.ConstantsData;
import dianmobile.eistarandroid.utils.HandleString;

/**
 * Created by dianmobile_yy on 15/10/26.
 */
public class MyMailsRequest extends ByhhAndroidRequest {

    public static void execute(Context context, RequestType requestType, RequestCallback callback) {
        executeSuper(context, requestType, null, callback);
    }

    public MyMailsRequest(int method, String url,  Map<String, String> params, RequestCallback callback, Response.ErrorListener errorListener) {
        super(method, url, params, callback, errorListener);
    }

    @Override
    protected void parseXmlToMap(XmlPullParser parser, Map<String, Object> result) {
        String TAG_MAIL = "mail";
        String TAG_FILENAME = "filename";
        String TAG_TYPE = "type";
        String TAG_USER_ID = "userid";
        String TAG_DATE = "date";
        String TAG_TITLE = "title";

        MailEntity entity = null;
        List<MailEntity> listData = new ArrayList<>();

        try {
            int event = parser.getEventType();
            while (event != XmlPullParser.END_DOCUMENT) {
                switch (event) {
                    case XmlPullParser.START_TAG:
                        String tagName = parser.getName();
                        if(TAG_MAIL.equals(tagName)) {
                            entity = new MailEntity();
                        } else if(TAG_FILENAME.equals(tagName) && entity != null) {
                            entity.setFileName(parser.nextText());
                        } else if(TAG_TYPE.equals(tagName) && entity != null) {
                            if(parser.nextText().contains("N")) {
                                entity.setIsRead(false);
                            } else {
                                entity.setIsRead(true);
                            }
                        } else if(TAG_USER_ID.equals(tagName) && entity != null) {
                            entity.setUserID(parser.nextText());
                        } else if(TAG_DATE.equals(tagName) && entity != null) {
                            entity.setSendTime(HandleString.getTimeDifference(parser.nextText()));
                        } else if(TAG_TITLE.equals(tagName) && entity != null) {
                            entity.setMailTitle(parser.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if(TAG_MAIL.equals(parser.getName())) {
                            listData.add(entity);
                            entity = null;
                        }
                        break;
                    default:
                        break;
                }
                event = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Collections.reverse(listData);//返回的是按时间从以前到现在排序，将其翻转
        result.put(ConstantsData.RESULT_DATA, listData);
    }
}
